package com.soft.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.soft.entity.Food;
import com.soft.mapper.FoodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class FoodService {

    @Autowired
    private FoodMapper foodMapper;

    /* ----------------- 基础查询 ----------------- */
    public List<Food> listAll() {
        return foodMapper.selectList(new QueryWrapper<>());
    }

    public Page<Food> listByCategoryPage(int page, int size, String category) {
        Page<Food> pageReq = new Page<>(page, size);
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        if (category != null && !"全部".equals(category)) {
            wrapper.eq("food_category", category);
        }
        wrapper.orderByAsc("food_id");
        return foodMapper.selectPage(pageReq, wrapper);
    }

    public List<Food> listByCategory(String category) {
        QueryWrapper<Food> wrapper = new QueryWrapper<>();
        if (category != null && !"全部".equals(category)) {
            wrapper.eq("food_category", category);
        }
        return foodMapper.selectList(wrapper);
    }

    public List<String> listCategories() {
        return foodMapper.selectList(new QueryWrapper<>())
                .stream().map(Food::getFoodCategory).distinct().toList();
    }

    /* ----------------- 获取单个菜品 ----------------- */
    public Food getById(Long foodId) {
        return foodMapper.selectById(foodId);
    }

    /* ----------------- 校验库存 & 状态 ----------------- */
    public String checkAndReduceStock(List<Food> orderItems) {
        for (Food item : orderItems) {
            Food dbFood = foodMapper.selectById(item.getFoodId());
            if (dbFood == null) {
                return "菜品【" + item.getFoodName() + "】不存在";
            }
            if (!"onsale".equals(dbFood.getFoodStatus())) {
                return "菜品【" + dbFood.getFoodName() + "】已下架";
            }
            if (dbFood.getFoodCount() < item.getFoodCount()) {
                return "菜品【" + dbFood.getFoodName() + "】库存不足，仅剩 " + dbFood.getFoodCount() + " 份";
            }
        }
        // 扣减库存
        for (Food item : orderItems) {
            Food dbFood = foodMapper.selectById(item.getFoodId());
            dbFood.setFoodCount(dbFood.getFoodCount() - item.getFoodCount());
            foodMapper.updateById(dbFood);
        }
        return "success";
    }

 // 检查购物车状态（不存在/下架/库存不足）
    public Map<Long, String> checkCart(List<Long> foodIds) {
        Map<Long, String> result = new HashMap<>();
        for (Long id : foodIds) {
            Food f = foodMapper.selectById(id);
            if (f == null) {
                result.put(id, "notfound"); // 菜品不存在
            } else if (!"onsale".equals(f.getFoodStatus())) {
                result.put(id, "notsale"); // 已下架
            } else if (f.getFoodCount() <= 0) {
                result.put(id, "nostock"); // 库存不足
            } else {
                result.put(id, "onsale"); // 可售
            }
        }
        return result;
    }

    /* ----------------- CRUD ----------------- */
    public int addFood(Food food) {
        return foodMapper.insert(food);
    }

    public int updateFood(Food food) {
        return foodMapper.updateById(food);
    }

    public int deleteFood(Long foodId) {
        return foodMapper.deleteById(foodId);
    }

    public int updateFoodStatus(Long foodId, String foodStatus) {
        if (foodId == null || foodStatus == null) return 0;
        if (!"onsale".equals(foodStatus) && !"notsale".equals(foodStatus)) return 0;

        Food dbFood = foodMapper.selectById(foodId);
        if (dbFood == null) return 0;
        if (foodStatus.equals(dbFood.getFoodStatus())) return 0;

        Food updateFood = new Food();
        updateFood.setFoodId(foodId);
        updateFood.setFoodStatus(foodStatus);
        return foodMapper.updateById(updateFood);
    }
}
